package org.jimmy.tang.collection.comparator;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author jimmy
 * 
 */
public class UserComparator implements Comparator<User> {

    @Override
    public int compare(User arg0, User arg1) {
        return arg0.getAge() - arg1.getAge();
    }

    /**
     * 测试方法
     */
    public static void main(String[] args) {
        User[] users = new User[] { new User("u1001", 25),
                new User("u1002", 20), new User("u1003", 21) };
        Arrays.sort(users, new UserComparator());
        for (int i = 0; i < users.length; i++) {
            User user = users[i];
            System.out.println(user.getId() + " " + user.getAge());
        }
    }
}

class User {

    private String id;
    private int age;

    public User(String id, int age) {
        this.id = id;
        this.age = age;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}